<template>
	<view class="goods-list flex flex-wrap justify-between">
		<goods-item 
			v-for="item in cakeList" 
			:gdata="item" 
			:key="item.objectId"
		/>
	</view>
</template>

<script>
	import { cakeGet } from '../../api/cake.js'
	import {ID,KEY,BASE} from '../../config/index.js'
	export default {
		data() {
			return {
				cakeList:[],
				page:0
			}
		},
		onLoad() {
			// 方法1：
			// uni.request({
			// 	url:`${BASE}/1.1/classes/CakeGoods`,
			// 	method:'GET',
			// 	header:{
			// 		'X-LC-Key':KEY,
			// 		'X-LC-ID':ID,
			// 		'content-type':'application/json'
			// 	},
			// 	success:(res)=>{
			// 		console.log(res);
			// 		this.cakeList = res.data.results
			// 	}
			// })
			// 方法2:
			cakeGet().then(res=>{
				this.cakeList = res.data.results
				this.page++
			})
		},
		onReachBottom() {
			console.log('触底了');
			cakeGet(this.page).then(res=>{
				let {results} = res.data
				if(results.length){
					this.cakeList = [
						...this.cakeList,
						...results
					]
					this.page++
					return
				}
				uni.showToast({
					title:'没有更多了',
					icon:'none'
				})
			})
		},
		onPullDownRefresh() {
			this.cakeList = []
			this.page = 0
			cakeGet().then(res=>{
				uni.stopPullDownRefresh() //关闭刷新动画
				this.cakeList = res.data.results
				this.page++
			})
		},
		methods: {
			
		}
	}
</script>

<style lang="scss">
.goods-list{
	padding: 16upx;
}
</style>
